package com.ctshk.rpc.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.NumberConstant;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataTouristTypeListDTO;
import com.ctshk.rpc.system.entity.MainDataTouristType;
import com.ctshk.rpc.system.mapper.MainDataPassengerTypeMapper;
import com.ctshk.rpc.system.req.*;
import com.ctshk.rpc.system.service.IMainDataTouristTypeService;
import com.ctshk.rpc.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author xuhui
 * @since 1/25/2021
 */
@DubboService
@Slf4j
public class MainDataTouristTypeServiceImpl extends ServiceImpl<MainDataPassengerTypeMapper, MainDataTouristType> implements IMainDataTouristTypeService {

    @Autowired
    private MainDataPassengerTypeMapper mainDataTouristTypeMapper;

    @DubboReference
    private ISysUserService sysUserService;

    @Override
    public PageResponse<MainDataTouristTypeListDTO> list(MainDataTouristTypeListReq mainDataTouristTypeListReq) {
        Page<MainDataTouristType> page = new Page<>(mainDataTouristTypeListReq.getPageNo(), mainDataTouristTypeListReq.getPageSize());
        QueryWrapper<MainDataTouristType> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(null != mainDataTouristTypeListReq.getStatus(), MainDataTouristType::getStatus, mainDataTouristTypeListReq.getStatus())
                .like(StringUtils.isNotBlank(mainDataTouristTypeListReq.getTouristTypeName()), MainDataTouristType::getTouristTypeName, mainDataTouristTypeListReq.getTouristTypeName())
                .eq(MainDataTouristType::getIsDeleted, IsDeletedCode.NO.getCode())
                .orderByAsc(MainDataTouristType::getSort);
        Page<MainDataTouristType> iPage = baseMapper.selectPage(page, wrapper);
        List<MainDataTouristType> mainDataTouristTypeList = iPage.getRecords();
        if (CollectionUtils.isEmpty(mainDataTouristTypeList)) {
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataTouristTypeListDTO> mainDataTouristTypeListDTOS = EntityUtil.copyList(mainDataTouristTypeList, MainDataTouristTypeListDTO.class);
        return new PageResponse<>(mainDataTouristTypeListDTOS, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }

    @Override
    public Result<Integer> add(MainDataTouristTypeAddReq req, TokenUser currentUser) {
        List<MainDataTouristType> list = mainDataTouristTypeMapper.selectList(new QueryWrapper<>());
        for (MainDataTouristType mdtt : list) {
            //起始年龄被包含
            if ((mdtt.getAgeBegin() < req.getAgeBegin() && mdtt.getAgeEnd() > req.getAgeBegin()) ||
                    mdtt.getAgeEnd() == req.getAgeBegin() || mdtt.getAgeBegin() == req.getAgeBegin()) {
                return Result.failed(SystemError.SYS_440);
            }
            //终止年龄被包含
            if ((mdtt.getAgeBegin() < req.getAgeEnd() && mdtt.getAgeEnd() > req.getAgeEnd()) ||
                    mdtt.getAgeEnd() == req.getAgeEnd() || mdtt.getAgeBegin() == req.getAgeEnd()) {
                return Result.failed(SystemError.SYS_440);
            }
        }
        //新增旅客类型
        MainDataTouristType mainDataTouristTypeAdded = EntityUtil.copy(req, MainDataTouristType.class);
        mainDataTouristTypeAdded.setCreateId(currentUser.getId());
        mainDataTouristTypeAdded.setGmtCreate(LocalDateTime.now());
        mainDataTouristTypeAdded.setIsDeleted(NumberConstant.ZERO.getValue());
        return new Result(mainDataTouristTypeMapper.insert(mainDataTouristTypeAdded));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result modify(MainDataTouristTypeModifyReq mainDataTouristTypeModifyReq, TokenUser currentUser) {
        List<MainDataTouristType> list = mainDataTouristTypeMapper.selectList(
                new QueryWrapper<MainDataTouristType>().lambda().ne(MainDataTouristType::getId,mainDataTouristTypeModifyReq.getId())
        );
        for (MainDataTouristType mdtt : list) {
            //起始年龄被包含
            if ((mdtt.getAgeBegin() < mainDataTouristTypeModifyReq.getAgeBegin() && mdtt.getAgeEnd() > mainDataTouristTypeModifyReq.getAgeBegin()) ||
                    mdtt.getAgeEnd() == mainDataTouristTypeModifyReq.getAgeBegin() || mdtt.getAgeBegin() == mainDataTouristTypeModifyReq.getAgeBegin()) {
                return Result.failed(SystemError.SYS_440);
            }
            //终止年龄被包含
            if ((mdtt.getAgeBegin() < mainDataTouristTypeModifyReq.getAgeEnd() && mdtt.getAgeEnd() > mainDataTouristTypeModifyReq.getAgeEnd()) ||
                    mdtt.getAgeEnd() == mainDataTouristTypeModifyReq.getAgeEnd() || mdtt.getAgeBegin() == mainDataTouristTypeModifyReq.getAgeEnd()) {
                return Result.failed(SystemError.SYS_440);
            }
        }
        MainDataTouristType touristType = EntityUtil.copy(mainDataTouristTypeModifyReq, MainDataTouristType.class);
        touristType.setModifiedId(currentUser.getId());
        touristType.setGmtModified(LocalDateTime.now());
        baseMapper.updateById(touristType);
        return Result.success(mainDataTouristTypeModifyReq.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result modifyStatus(MainDataTouristTypeModifyStatusReq mainDataTouristTypeModifyStatusReq, TokenUser currentUser) {
        QueryWrapper<MainDataTouristType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", mainDataTouristTypeModifyStatusReq.getId());
        MainDataTouristType mainDataTouristType = mainDataTouristTypeMapper.selectOne(queryWrapper);
        if (mainDataTouristType != null) {
            mainDataTouristType.setStatus(mainDataTouristTypeModifyStatusReq.getStatus());
            return new Result(mainDataTouristTypeMapper.updateById(mainDataTouristType));
        }
        if (mainDataTouristType == null) {
            throw new BusinessException(SystemError.MAINDATA_4021);
        }
        return Result.success();
    }

    @Override
    public MainDataTouristTypeListDTO query(MainDataTouristTypeQueryReq req) {
        QueryWrapper<MainDataTouristType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataTouristType::getId, req.getId());
        MainDataTouristType mainDataTouristType = baseMapper.selectOne(queryWrapper);
        MainDataTouristTypeListDTO dto = EntityUtil.copy(mainDataTouristType, MainDataTouristTypeListDTO.class);
        return dto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delete(Long id, TokenUser del) {
        QueryWrapper<MainDataTouristType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("is_deleted", 0);
        Integer count = mainDataTouristTypeMapper.selectCount(queryWrapper);
        if (count.equals(1)) {
            MainDataTouristType mainDataTouristType = new MainDataTouristType();
            mainDataTouristType.setId(id);
            mainDataTouristType.setIsDeleted(NumberConstant.ONE.getValue());
            return new Result(mainDataTouristTypeMapper.updateById(mainDataTouristType));
        }
        if (count.equals(0)) {
            throw new BusinessException(SystemError.MAINDATA_4019);
        }

        return Result.success();
    }

}